clear all
cap close
set more off

cd ".\Data_Program"

use county,clear

* merge with division data
merge m:1 county using Econdata\countytodivision
keep if _merge==3 
drop _merge

* merge with neighbor data
merge 1:1 quarter county using sci_weighted_iv.dta
keep if _merge==3
drop _merge

* merge with Mintel data (proprietary)
merge 1:1 county quarter using Proprietary\mintel
drop if _merge==2 
drop _merge 
replace finad=0 if finad==.
replace nonfinad=0 if nonfinad==.
replace ad=0 if ad==.
gen finadshare = finad/ad

* merge with own economic data

    * CoreLogic house price growth (proprietary)
merge 1:1 quarter county using Proprietary\hpi_county_quarter_exp
drop if _merge==2
drop _merge
drop qhpi
replace hpgr=hpgr4

    * Census Buearu population growth
gen year=yofd(dofq(quarter))	
merge m:1 year county using Econdata\pop_county
drop if _merge==2
drop _merge
drop fracm frach

    * QCEW employment growth
merge m:1 year county using Econdata\emp_county_year
drop if _merge==2
drop _merge

    * CCP subprime share
merge 1:1 quarter county using Proprietary\subprime_county_quarter
drop if _merge==2  
drop _merge 

     * Census education level in 2010
merge m:1 county using Econdata\edu
drop if _merge==2  
drop _merge 

* demographic X year controls
tab year, gen(yy)
foreach i of numlist 1/10{
    gen baboveyy`i' = babove*yy`i'
	gen babove2yy`i' = babove2*yy`i'
	gen babove2_inXyy`i' = babove2_inX*yy`i'
	gen fracbyy`i' = fracb*yy`i'
	gen fracyyy`i' = fracy*yy`i'
}


* IRS migration flows
merge m:1 year county using Econdata\migration
drop if _merge==2  
drop _merge


* Regressions
xtset county quarter

* weights
sort county quarter
by  county: egen w1 = mean(tot)
by  county: egen w2 = mean(pop)
gen w=w1

scalar h = 4

  * outcome var
foreach x of varlist finshare share_refi finshare_refi nonbshare_refi nbnfshare_refi bankshare_refi finadshare{
gen	d`x'=`x'-l`=h'.`x'
gen	d`x'2=`x'2-l`=h'.`x'2
gen	d`x'2_outcz=`x'2_outcz-l`=h'.`x'2_outcz
}

gen dfinshare_pur =finshare_pur-l`=h'.finshare_pur
gen dfin=(fin-l`=h'.fin)/l`=h'.tot
gen dfin_refi=(finrefi-l`=h'.finrefi)/l`=h'.tot
gen dfin_pur=(finpur-l`=h'.finpur)/l`=h'.tot
gen dnonfin=((tot-fin)-(l`=h'.tot-l`=h'.fin))/l`=h'.tot
gen dtot = tot/l`=h'.tot-1

gen dlogfin = log(fin+1)-log(l`=h'.fin+1)
gen dlogfinrefi = log(finrefi+1)-log(l`=h'.finrefi+1)
gen dlogfinpur = log(finpur+1)-log(l`=h'.finpur+1)

gen dfinad = (log(finad+1)-log(l`=h'.finad+1))/100
gen dfinad2 = (log(finad2+1)-log(l`=h'.finad2+1))/100
gen dfinad2_outcz = (log(finad2_outcz+1)-log(l`=h'.finad2_outcz+1))/100
gen dfinad2_inX = (log(finad2_inX+1)-log(l`=h'.finad2_inX+1))/100

gen dfinshare2_outst=finshare2_outst-l`=h'.finshare2_outst  
gen dfinshare2_outdis100=finshare2_outdis100-l`=h'.finshare2_outdis100 
gen dfinshare2_outdis200=finshare2_outdis200-l`=h'.finshare2_outdis200 
gen dfinshare2_outcz_out10 = finshare2_outcz_out10-l`=h'.finshare2_outcz_out10
gen dfinshare2_outcz_out30 = finshare2_outcz_out30-l`=h'.finshare2_outcz_out30
gen dfinshare2_out10 = finshare2_out10-l`=h'.finshare2_out10
gen dfinshare2_out30 = finshare2_out30-l`=h'.finshare2_out30

gen dfinshare2_outcz_inX = finshare2_outcz_inX-l`=h'.finshare2_outcz_inX
gen dfinshare2_inX = finshare2_inX-l`=h'.finshare2_inX

global control1 hpgr popgr empgr fracyyy* fracbyy* subprime baboveyy* dfinad linflow loutflow
global control2 hpgr popgr empgr fracyyy* fracbyy* subprime baboveyy* dfinad linflow loutflow hpgr2 popgr2 empgr2 fracy2 fracb2 subprime2 babove2yy* dfinad2 
global control3 hpgr popgr empgr fracyyy* fracbyy* subprime baboveyy* linflow loutflow hpgr2 popgr2 empgr2 fracy2 fracb2 subprime2 babove2yy* 

save beforereg_sci_iv,replace


*===============================================================================
* Figure 3. Dynamic cumulative effects
*===============================================================================

use beforereg_sci_iv,clear

tempname dyn_all
postfile `dyn_all' h b_all se_all using dyn_all.dta,replace

tempname dyn_refi
postfile `dyn_refi' h b_refi se_refi using dyn_refi.dta,replace

tempname dyn_pur
postfile `dyn_pur' h b_pur se_pur using dyn_pur.dta,replace

forval h= 1/10{
	
drop dfinshare dfinshare2 dfinshare2_outcz dfinshare_refi dfinshare_pur
gen	dfinshare = finshare-l`h'.finshare
gen	dfinshare_refi = finshare_refi-l`h'.finshare_refi
gen	dfinshare_pur = finshare_pur-l`h'.finshare_pur
gen	dfinshare2 = finshare2-l`h'.finshare2
gen	dfinshare2_outcz = finshare2_outcz-l`h'.finshare2_outcz

ivreghdfe dfinshare (dfinshare2=dfinshare2_outcz) $control2 [aw=w] , a(division#quarter county) cluster(county) 
post `dyn_all' (`h') (_b[dfinshare2]) (_se[dfinshare2])

ivreghdfe dfinshare_refi (dfinshare2=dfinshare2_outcz) $control2 [aw=w] , a(division#quarter county) cluster(county) 
post `dyn_refi' (`h') (_b[dfinshare2]) (_se[dfinshare2])


ivreghdfe dfinshare_pur (dfinshare2=dfinshare2_outcz) $control2 [aw=w] , a(division#quarter county) cluster(county) 
post `dyn_pur' (`h') (_b[dfinshare2]) (_se[dfinshare2])
}

postclose `dyn_all'
postclose `dyn_refi'
postclose `dyn_pur'


use dyn_all,clear
merge 1:1 h using dyn_refi
drop _merge
merge 1:1 h using dyn_pur
drop _merge

scalar d = 1.96
local varlist="all refi pur"
foreach x of local varlist{
	gen lb_`x'=b_`x'-d*se_`x'
	gen ub_`x'=b_`x'+d*se_`x'
}


gen h1=h+0.25

* Figure 3(a)
twoway(scatter b_all h, msymbol(S) mcolor(black) )(rcap lb_all ub_all h, lcolor(black)), graphregion(color(white)) xtitle("Quarter",size(5)) ytitle("Cumulative effect (pp)",size(5))   xlabel(1(1)10.5,labsize(5) nogrid) ylabel(0(0.1)0.55,labsize(5) ) legend(off) scheme(s2color)

* Figure 3(b)
twoway(scatter b_refi h, msymbol(D) mcolor(navy))(rcap lb_refi ub_refi h, lcolor(navy))(scatter b_pur h1, msymbol(T) mcolor(maroon))(rcap lb_p ub_p h1, lcolor(maroon)), graphregion(color(white)) xtitle("Quarter",size(5)) ytitle("Cumulative effect (pp)",size(5))  legend(order(1 "Refinancing" 3 "Home Purchasing") ring(0) pos(5) region(color(none)) row(2) size(4.5)) xlabel(1(1)10.5,labsize(5) nogrid) ylabel(0(0.1)0.55,labsize(5)) scheme(s2color)
